package reno.activity.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import reno.activity.dao.ActivityDao;
import reno.activity.eo.ActivityEntity;
import reno.activity.eo.ActivitydetailEntity;
import reno.activity.mo.*;

import java.io.File;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.util.*;
import java.util.regex.Pattern;

@Service
public class ActivityService {

    private static final Pattern BAN = Pattern.compile("^BAN.+$");

    @Autowired
    ActivityDao activityDao;

    public String buildActivity(Long activityid) {

        ActivityEntity activityEntity = activityDao.getActivityInfo(activityid);
        if (activityEntity != null) {
            StringBuilder stringBuilder = new StringBuilder("奖池内容：\n");
            List<ActivitydetailEntity> activitydetailEntityList = activityDao.getActivityDetailInfo(activityid);
            if (activitydetailEntityList.size() > 0) {
                List<Reward> rewardList = new ArrayList<>();
                for (ActivitydetailEntity eachDetailEntity : activitydetailEntityList) {
                    //BAN系列奖品不列入奖品列表。
                    if (!BAN.matcher(eachDetailEntity.getRewardname()).matches()) {
                        stringBuilder.append(eachDetailEntity.getRewardname()).append(" ").append(eachDetailEntity.getRewardcount()).append("份\n");
                    }
                    for (int i = 0; i < eachDetailEntity.getRewardcount(); i++) {
                        Reward reward = new Reward(eachDetailEntity.getRewardname(), null, eachDetailEntity.getRewardid());
                        rewardList.add(reward);
                    }
                }
                activityEntity.setOpen(true);
                ActivityPool.getInstance().getActivityPoolMap().put(activityid, activityEntity);
                RewardPool rewardPool = RewardPool.getInstance();
                rewardPool.getRewardPoolMap().put(activityid, rewardList);
                Map<Long, Reward> rewardMap = new HashMap<>();
                RewardPool.getInstance().getRewardResultMap().put(activityid, rewardMap);
            }
            stringBuilder.append("活动[").append(activityEntity.getActivityname()).append("]开启成功");
            return stringBuilder.toString();
        }
        return null;
    }

    public ActivityEntity getActivityInfo(Long activityid) {
        return activityDao.getActivityInfo(activityid);

    }

    public String rewardListToString(List<Reward> rewardList) {
        StringBuffer stringBuffer = new StringBuffer();
        for (Reward reward : rewardList) {
            stringBuffer.append(reward.toCardString()).append("\n");
        }
        return stringBuffer.toString();
    }

    public Map<Long, Member> buildGroupCardMap(List<Member> memberList) {
        Map<Long, Member> groupCardMap = new HashMap<>();
        for (Member eachMember : memberList) {
            groupCardMap.put(eachMember.getQqId(), eachMember);
        }
        return groupCardMap;
    }

    public String rewardMapToString(Map<Long, Reward> rewardMap) {
        List<Reward> list = new ArrayList();
        // 获得map的Iterator
        Iterator iter = rewardMap.entrySet().iterator();
        while (iter.hasNext()) {
            Map.Entry entry = (Map.Entry) iter.next();
            list.add((Reward) entry.getValue());
        }
        list.sort(this::compare);
        return rewardListToString(list);
    }

    private int compare(Reward o1, Reward o2) {
        if (o1.getOrder() > o2.getOrder()) {
            return 1;
        } else if (o1.getOrder() < o2.getOrder()) {
            return -1;
        }
        return 0;
    }

    public void writeFile(List<Reward> rewardList, String activityID) {
        StringBuilder stringBuilder = new StringBuilder("剩余奖励物品:\n");
        Map<String, Integer> surplus = new HashMap<>();
        for (Reward eachReward : rewardList) {
            surplus.merge(eachReward.getRewardname(), 1, (a, b) -> a + b);
        }
        // 获得map的Iterator
        Iterator iter = surplus.entrySet().iterator();
        while (iter.hasNext()) {
            Map.Entry entry = (Map.Entry) iter.next();
            stringBuilder.append(entry.getKey()).append(" ").append(entry.getValue()).append("份\n");
        }
        writeFile(stringBuilder.toString(), activityID + "剩余奖励物品");
    }

    public void writeFile(Map<Long, Reward> rewardMap, String activityID) {
        writeFile("已中奖列表:\n" + rewardMapToString(rewardMap), activityID + "已中奖列表");
    }

    private void writeFile(String str, String fileName) {
        File myFilePath = new File("C:\\file\\" + fileName + ".txt");
        try {
            if (!myFilePath.exists()) {
                myFilePath.createNewFile();
            }
            FileWriter resultFile = new FileWriter(myFilePath);
            PrintWriter myFile = new PrintWriter(resultFile);
            myFile.println(str);
            resultFile.close();
        } catch (Exception e) {
            System.out.println("新建文件操作出错");
            e.printStackTrace();
        }
    }

    public Boolean checkAuth(ActivitySwitchMO activitySwitchMO) {
        ActivityEntity activityEntity = ActivityPool.getInstance().getActivityPoolMap().get(activitySwitchMO.getActivityid());
        if (activityEntity == null) {
            return false;
        }
        String[] splitAuth = activityEntity.getManager().split("\\|");
        List<String> authList = Arrays.asList(splitAuth);
        for (String eachAuth : authList) {
            System.out.println(eachAuth);
        }
        return authList.contains(activitySwitchMO.getRequestQQ().toString());
    }
}
